<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<!-- 
    localStorage:本地存储 特点:

        1.声明周期永久生效,除非手动删除 否则关闭页面也会存在
        2.可以多窗口(页面) 共享 (同一浏览器可以共享)
        3.以键值对的形式存储使用


    localStorage.
        setItem(key,value)   // 存储数据
        getItem(key)        // 获取数据
        removeItem(key)     // 删除数据
        clear()             // 删除所有数据
 -->

<body>
    <input type="text">
    <button class="set">存储数据</button>
    <button class="get">获取数据</button>
    <button class="remove">删除数据</button>
    <button class="del">清空所有数据</button>
</body>
<script>
    var ipt = document.querySelector('input')
    var set = document.querySelector('.set')
    var get = document.querySelector('.get')
    var remove = document.querySelector('.remove')
    var del = document.querySelector('.del')

    console.log(localStorage);
    var obj = { name: 'Tom', age: 18 }
    set.addEventListener('click', function () {

        //存储数据
        localStorage.setItem('userName', ipt.value);
        localStorage.setItem('pwd', 888)
        localStorage.setItem('obj', JSON.stringify(obj))
    })

    get.addEventListener('click', function () {

        //获取数据
        console.log(localStorage.getItem('userName'));
        console.log(localStorage.getItem('pwd'));
        console.log(JSON.parse(localStorage.getItem('obj')));

    })

    remove.addEventListener('click', function () {
        // 删除数据
        localStorage.removeItem('pwd')
    })

    del.addEventListener('click', function () {
        //清空数据
        localStorage.clear()
    })
</script>
<!-- 
    其他页面 也可以调用
 -->
<!-- 

 <body>
      <button class="get">获取数据</button>
    <script>
        document.querySelector('.get').addEventListener('click', function () {
            var obj = localStorage.getItem('obj');
            obj = JSON.parse(obj);
            console.log(obj);
        })
    </script>
 </body>

 -->

</html>